package com.woniu.liuzhougov.itemAndTask.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.liuzhougov.authorityControl.pojo.User;
import com.woniu.liuzhougov.authorityControl.service.UserService;
import com.woniu.liuzhougov.authorityControl.utils.TableResult;
import com.woniu.liuzhougov.authorityControl.verification.AddDepartment;
import com.woniu.liuzhougov.authorityControl.verification.EditDepartment;
import com.woniu.liuzhougov.authorityControl.verification.SetDepartmentLeader;
import com.woniu.liuzhougov.common.utils.Result;
import com.woniu.liuzhougov.common.utils.ValidatedResult;
import com.woniu.liuzhougov.itemAndTask.pojo.Department;
import com.woniu.liuzhougov.itemAndTask.service.DepartmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0
 * @Author zhuxinpan
 * @ClassName DepartmentController
 * @Description 处理与department相关请求
 * @Date 2019/4/23 14:46
 */
@Controller
@RequestMapping("department")
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    @Resource
    private UserService userService;

    /**
     * @return java.util.List<com.woniu.liuzhougov.itemAndTask.pojo.Department>
     * @author zhuxinpan
     * @description
     * @date 2019/4/26 11:08
     * @params []
     */
    @GetMapping("/listDepartments")
    @ResponseBody
    public List<Department> listDepartments() {
        return departmentService.listDepartments();
    }

    /**
     * @param currentPage
     * @param pageSize
     * @return com.woniu.liuzhougov.authorityControl.utils.TableResult<com.woniu.liuzhougov.itemAndTask.pojo.Department>
     * @author zhuxinpan
     * @date 2019/4/28 22:07
     * @description 分页展示所有部门
     */
    @PostMapping("/listDepartments")
    @ResponseBody
    public TableResult<Department> listDepartments(@RequestParam(name = "page", required = false, defaultValue = "1") Integer currentPage, @RequestParam(name = "limit", required = false, defaultValue = "5") Integer pageSize) {
        List<Department> departmentList = departmentService.listDepartments(currentPage, pageSize);
        PageInfo<Department> pageInfo = new PageInfo<>(departmentList);
        TableResult<Department> tableResult = TableResult.getTableResult(departmentList, pageInfo.getTotal());
        return tableResult;
    }

    /**
     * @param
     * @return java.lang.String
     * @author zhuxinpan
     * @date 2019/4/28 21:41
     * @description 跳转到管理界面
     */
    @RequiresPermissions("department_manager")
    @GetMapping("/manager")
    public String manager() {
        return "department_manager";
    }

    /**
     * @param
     * @return java.lang.String
     * @author zhuxinpan
     * @date 2019/4/28 23:04
     * @description 跳转到添加页面
     */
    @RequiresPermissions("department_addDepartment")
    @GetMapping("/addDepartment")
    public String addDepartment() {
        return "department_manager_addDepartment";
    }

    /**
     * @param department
     * @param bindingResult
     * @return com.woniu.liuzhougov.common.utils.Result
     * @author zhuxinpan
     * @date 2019/4/28 23:09
     * @description 添加部门
     */
    @RequiresPermissions("department_addDepartment")
    @PostMapping("/addDepartment")
    @ResponseBody
    public Result addDepartment(@Validated(value = {AddDepartment.class}) Department department, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.error(ValidatedResult.getValidateMessage("添加部门失败!", bindingResult));
        }
        if (departmentService.countByDepartmentName(department)>0) {
            return Result.error("该部门名称已存在!");
        }
        if (departmentService.insertDepartment(department) == 0) {
            return Result.error("添加部门失败!");
        }
        return Result.success("添加部门成功!");
    }

    /**
     * @param departmentId
     * @param model
     * @return java.lang.String
     * @author zhuxinpan
     * @date 2019/4/28 23:54
     * @description 跳转到编辑部门界面
     */
    @RequiresPermissions("department_editDepartment")
    @GetMapping("/editDepartment/{departmentId}")
    public String editDepartment(@PathVariable(name = "departmentId") Integer departmentId, Model model) {
        Department department = departmentService.getDepartmentByDepartmentId(departmentId);
        model.addAttribute("department", department);
        return "department_manager_editDepartment";
    }

    /**
     * @param department
     * @param bindingResult
     * @return com.woniu.liuzhougov.common.utils.Result
     * @author zhuxinpan
     * @date 2019/4/29 0:18
     * @description 编辑部门
     */
    @RequiresPermissions("department_editDepartment")
    @PostMapping(value = "/editDepartment")
    @ResponseBody
    public Result editDepartment(@Validated(value = EditDepartment.class) Department department, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.error(ValidatedResult.getValidateMessage("编辑部门失败!", bindingResult));
        }
        if ("".equals(department.getDepartmentName().trim())) {
            return Result.error("部门名不能为空!");
        }
        if (departmentService.countByDepartmentName(department) > 0) {
            return Result.error("该部门名重复!");
        }
        if (departmentService.updateDepartment(department) == 0) {
            return Result.error("修改部门失败!");
        }
        return Result.success("修改部门成功!");
    }

    /**
     * @return org.springframework.web.servlet.ModelAndView
     * @Author zhuxinpan
     * @Description 处理请求, 跳转到更新部门页面
     * @date 2019/4/24 16:49
     * @params [userId]
     */
    @RequiresPermissions("department_setLeader")
    @GetMapping("/setLeader/{departmentId}")
    public String setLeader(@PathVariable(name = "departmentId") Integer departmentId, Model model) {
        Department department = departmentService.getDepartmentByDepartmentId(departmentId);
        List<User> userList = userService.listUserByDepartmentId(departmentId);
        model.addAttribute(department);
        model.addAttribute(userList);
        return "department_manager_setLeader";
    }

    /**
     * @param department
     * @param bindingResult
     * @return com.woniu.liuzhougov.common.utils.Result
     * @author zhuxinpan
     * @date 2019/4/29 0:34
     * @description 设置部门领导
     */
    @RequiresPermissions("department_setLeader")
    @RequestMapping("/setLeader")
    @ResponseBody
    public Result setLeader(@Validated(value = {SetDepartmentLeader.class}) Department department,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.error(ValidatedResult.getValidateMessage("设置部门领导失败!",bindingResult));
        }
        if (departmentService.updateLeaderId(department) == 0) {
            return Result.error("设置部门领导失败");
        }
        return Result.success("设置部门领导成功!");
    }

    /**
     * @param departmentId
     * @return com.woniu.liuzhougov.common.utils.Result
     * @author zhuxinpan
     * @date 2019/4/29 1:01
     * @description 删除部门
     */
    @RequiresPermissions("department_deleteDepartment")
    @DeleteMapping("/deleteDepartment/{departmentId}")
    @ResponseBody
    public Result deleteDepartment(@PathVariable(name = "departmentId") Integer departmentId) {
        if (userService.countRecordByDepartmentId(departmentId) > 0) {
            return Result.error("该部门还有人员,不能删除!");
        }
        if (departmentService.deleteDepartmentByDepartmentId(departmentId) == 0) {
            return Result.error("删除部门失败");
        }
        return Result.success("删除部门成功");
    }




}
